// pages/dictation/dictation.js
// 进度条
var passed
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //进度
    percent: 1,
    // 单词
    content: null,
    // 音标
    pron: null,
    // 词性
    definition: null,
    Id: 0,
    // 读音src
    audioURL: null,
    // 单词的翻译是否显示
    showNot: false,
    // 听写的单词词库
    dictation_word: [],
    // 计数器
    count: 0,
    // 是否显示数据
    showNot: false,
    // 任务目标单词数
    goal: 0,
    // 记录当前的随机数
    idx: 0,
    // 是否显示答案按钮
    showanser:false,
    // 听写情况
    anser_record:0,
    // 输入的单词
    wordinput:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 进度条为0
    passed = 0;
    // 选择的词库
    var list2 = require('../../data/cet4.js')
    var list3 = require('../../data/cet6.js')
    var list4 = require('../../data/kaoyan.js')
    var list5 = require('../../data/cet4_import.js')
    var list6 = require('../../data/cet6_import.js')
    var list7 = require('../../data/kaoyan_import.js')

    var checked = wx.getStorageSync("checked")
    console.log(checked);
    switch (checked) {
      case 'cet4':
        this.setData({
          list: list2,
        });
        break;
      case 'cet6':
        this.setData({
          list: list3,
        });
        break;
      case '考研':
        this.setData({
          list: list4,
        });
        break;
      case 'cet4高频':
        this.setData({
          list: list5,
        });
        break;
      case 'cet6高频':
        this.setData({
          list: list6,
        });
        break;
      case '考研高频':
        this.setData({
          list: list7,
        });
        break;
    }

    // 获取缓存中的id值
    var id = wx.getStorageSync("previous")
    console.log("id值", id)
    var goal = wx.getStorageSync("goal")
    console.log("goal值", goal)
    var dictation_word
    // dictation_word: this.data.list.wordList.slice(0, id)
    this.setData({
      dictation_word: this.data.list.wordList.slice(0, id),
      Id: id,
      goal: goal,
    })
    console.log("听写的词库", this.data.dictation_word);

    // 随机选取已学的单词听写
    this.random()
    // 获取单词的相关信息
    this.getaudiourl()

  },

  // 查看答案
  show: function () {
    this.setData({
      showNot: !this.data.showNot
    })
  },

  // 获取随机数
  random: function () {
    // 生成取随机数的数组
    var arr = new Array(this.data.Id)
    var i = arr.length
    while (i--) {
      arr[i] = i
    }
    this.setData({
      array: arr
    })
    //console.log(arr)
// 交换法获取不重复的随机数的数组
var result = [ ];
var ranNum = this.data.goal;
for (var i = 0; i < ranNum; i++) {
var ran = Math.floor(Math.random() * (arr.length - i));
result.push(arr[ran]);
arr[ran] = arr[arr.length - i - 1];
};
      this.setData({
        result:result
      })
  },
  // 获取单词的相关信息
  getaudiourl: function () {
    // 随机选取已学的单词听写
    // this.random()
    var idx = this.data.result[this.data.count]
    let that = this;

    //console.log("随机值", idx)
    console.log("计数器：",this.data.count)
      wx.request({
        url: 'https://api.shanbay.com/bdc/search/?word=' + this.data.dictation_word[idx],
        data: {},
        method: 'GET',
        success: function (res) {
          console.log(res)
          that.setData({
            content: res.data.data.content,
            audioURL: res.data.data.audio,
            pron: '/' + res.data.data.pronunciation + '/',
            definition: res.data.data.definition,
          })
          that.read(); //页面加载时，播放发音
        }
      })
      this.setData({
        idx: idx
      })
    // 计数器加一
    var count = this.data.count
    this.setData({
      count: ++count
    })
  },
  // 单词发声
  read() {
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.data.audioURL
    innerAudioContext.play()
    innerAudioContext.onPlay(() => {})
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },

  // 下一个单词
  next: function (e) {
    // 判断任务数是否大于已学的单词数
    if (this.data.Id < this.data.goal) {
      this.setData({
        goal: this.data.Id
      })
      wx.showToast({
        title: '已学单词数小于任务数，已自动设置为已学数'
      })
    }
      //判断是否填写了单词
      if(this.data.wordinput==''){
        wx.showToast({
          title: '请填写单词',
          icon: 'none',
          duration: 1500
        })
      }else{
       // 判断是否为最后一个单词
    if (this.data.count * 1 + 1 <= this.data.goal) {
      this.getaudiourl();
      passed++
      this.setData({
        //设置加载条
        display: false,
        percent: passed * 100 / (this.data.goal * 1 - 1),
        showanser:false,
        showNot:false,
        wordinput:''
      })
    } 
    else {
      var goal = this.data.goal;
      var anser_record = this.data.anser_record;
      var score = ((goal * 1 - anser_record * 1) / (goal * 1)) * 100
      this.setData({
        score: score.toFixed(1)
      })
      wx.showModal({
        title: '听写结束',
        content:"正确率："+ String(this.data.score) + '%',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {

          }
        },
        fail: () => { },
        complete: () => { }
      });
    }
      }
  },

  // 听写单词
  wordinput:function(e){
      var wordinput=e.detail.value
      console.log("输入的单词",wordinput)
      this.setData({
        wordinput:wordinput
      })
      if(wordinput!=this.data.content){
        wx.showToast({
          title:'错误',
          icon: 'none',
          duration: 1500,
        })
        var anser_record=this.data.anser_record
        this.setData({
          showanser:true,
          anser_record:anser_record*1+1
        })
      }else{
        this.setData({
          showNot:true,
        })
        wx.showToast({
          title:'正确',
          icon: 'success',
          duration: 800,
        })
      }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.setStorageSync("anser_record", this.data.anser_record);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      
        
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})